package com.android.bolos;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.util.Log;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;

import com.android.bolos.DayMonthYear;
import com.android.bolos.HourMinute;
import com.android.bolos.CustomDateTimePickerHelper;

public class Previoconcurso extends Activity {

	public static EditText  lugar;
	static int idseleccionado;
	public Button button1, button2;
	CustomDateTimePickerHelper CustomDateGallery;
    DayMonthYear galleryHolderDayMonthYear;
    HourMinute galleryHolderHourMinute;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.previoconcurso);
	
	    // TODO Auto-generated method stub
	    // Capturamos los objetos


	    this.button1 = (Button) findViewById(R.id.button1);
	    Previoconcurso.lugar = (EditText) findViewById(R.id.lugar);

	    Spinner tipo = (Spinner) findViewById(R.id.tipo);
	    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
	            this, R.array.tipos, android.R.layout.simple_spinner_item);
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    tipo.setAdapter(adapter);


	    tipo.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
	        public void onItemSelected(AdapterView<?> parent,
	            android.view.View v, int position, long id) {
	                idseleccionado = position;
	            }

	            public void onNothingSelected(AdapterView<?> parent) {
	                idseleccionado = 1000;
	            }
	        });
    
	    
	    

	    
	    // dia y hora
	    CustomDateTimePickerHelper customDateTimePickerHelper = new CustomDateTimePickerHelper(getApplicationContext());

        // year month date holder object
        // you could also use seperate Gallery objects if you like
        galleryHolderDayMonthYear = new DayMonthYear();
        galleryHolderDayMonthYear.mDay = (Gallery) findViewById(R.id.GalleryDay);
        galleryHolderDayMonthYear.mMonth = (Gallery) findViewById(R.id.GalleryMonth);
        galleryHolderDayMonthYear.mYear = (Gallery) findViewById(R.id.GalleryYear);
        galleryHolderDayMonthYear = customDateTimePickerHelper.getYearMonthDay(galleryHolderDayMonthYear, R.layout.galleryitem, R.id.datepickergallery_textView, 1);

        // hour minute
        galleryHolderHourMinute = new HourMinute();
        galleryHolderHourMinute.mHour = (Gallery) findViewById(R.id.GalleryHour);
        galleryHolderHourMinute.mMinute = (Gallery) findViewById(R.id.GalleryMinute);
        galleryHolderHourMinute = customDateTimePickerHelper.getHourMinute(galleryHolderHourMinute, R.layout.galleryitem, R.id.datepickergallery_textView);

        // you can use any Gallery related built in method
        galleryHolderDayMonthYear.mDay.setOnItemSelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                    // TODO Auto-generated method stub
                    Log.d("Dia test", (String) galleryHolderDayMonthYear.mDay.getItemAtPosition(arg2));
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                    // TODO Auto-generated method stub
            }
    });
        
        galleryHolderDayMonthYear.mMonth.setOnItemSelectedListener(new OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                        // TODO Auto-generated method stub
                        Log.d("Mestest", (String) galleryHolderDayMonthYear.mMonth.getItemAtPosition(arg2));

                
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                        // TODO Auto-generated method stub
                }
        });

        galleryHolderDayMonthYear.mDay.getSelectedItemPosition();
        
        /**
         * Helper method for getting date in a String
         */
        Log.d("Hour and minute: ", customDateTimePickerHelper.getHourMinute(galleryHolderHourMinute));

        
        /**
         * Helper method for gettin hour and minute in String
         */
        Log.d("Year month day: ", customDateTimePickerHelper.getYearMonthDay(galleryHolderDayMonthYear));
        
        
        
        button1.setOnClickListener(new OnClickListener() {
    	  	



    		public void onClick(View v) {
    			// aver si lee la fecha
    	        int month = (galleryHolderDayMonthYear.mMonth.getSelectedItemPosition() + 1 );
    	        int day = (Integer.valueOf(String.valueOf(galleryHolderDayMonthYear.mDay.getSelectedItem())) );
    	        StringBuilder sb = new StringBuilder();
    	        if (day < 10) {
    	            sb.append('0').append(day).append('/');
    	    } else {
    	            sb.append(day).append('/');
    	    }
    	        if (month < 10) {
    	                sb.append('0').append(month).append('/');
    	        } else {
    	                sb.append(month).append('/');
    	        }
    	        
    	        sb.append(Integer.valueOf(String.valueOf(galleryHolderDayMonthYear.mYear.getSelectedItem())));
    	       
    	    // termina de leer la fecha
    	        // ahora a ver si lle la hora
    	        int hour = Integer.valueOf(String.valueOf(galleryHolderHourMinute.mHour.getSelectedItem()));
                int minute = Integer.valueOf(String.valueOf(galleryHolderHourMinute.mMinute.getSelectedItem()));
                StringBuilder sbFrom = new StringBuilder();
                if (hour < 10) {
                        sbFrom.append('0').append(hour).append(':');
                } else {
                        sbFrom.append(hour).append(':');
                }
                if (minute < 10) {
                        sbFrom.append('0').append(minute);
                } else {
                        sbFrom.append(minute);
                } 
    	        //termina la hora
    	        final Bundle b = new Bundle();
    	        
    			b.putString("hora", sbFrom.toString());
    			b.putString("fecha", sb.toString());
    	              Intent intent = new Intent(Previoconcurso.this, Concurso.class);
    	              intent.putExtras(b);
    	              startActivity(intent);
    	           }
    	        });
	}

}
